package com.atstudying.mall.member.controller;

import com.atstudying.common.exception.BigCodeEnume;
import com.atstudying.common.utils.PageUtils;
import com.atstudying.common.utils.R;
import com.atstudying.mall.member.entity.MemberEntity;
import com.atstudying.mall.member.exception.PhoneExistException;
import com.atstudying.mall.member.exception.UsernameExistException;
import com.atstudying.mall.member.service.MemberService;
import com.atstudying.mall.member.vo.MemberLoginVo;
import com.atstudying.mall.member.vo.MemberRegistVo;
import com.atstudying.mall.member.vo.SocicalUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 会员
 *
 * @author zlj
 * @email 2908434305@qq.com
 * @date 2020-10-04 22:34:03
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo vo){

        try {
            memberService.regist(vo);

        }catch (UsernameExistException e){

            System.out.println(e);
            return R.error(BigCodeEnume.USER_EXIST_EXCEPTION.getCode(),BigCodeEnume.USER_EXIST_EXCEPTION.getMsg());

        }catch (PhoneExistException e){
            System.out.println(e);
            return R.error(BigCodeEnume.PHONE_EXIST_EXCEPTION.getCode(),BigCodeEnume.PHONE_EXIST_EXCEPTION.getMsg());


        }

        return R.ok();
    }

    @PostMapping("/oauth2/login")
    public R oauth2login(@RequestBody SocicalUser socicalUser) throws Exception {


        MemberEntity memberEntity= memberService.loginOauth2(socicalUser);
        if (memberEntity!=null){

            return R.ok().setData(memberEntity);

        }else {
            return R.error(BigCodeEnume.USERORPASSWORD_EXIST_EXCEPTION.getCode(),BigCodeEnume.USERORPASSWORD_EXIST_EXCEPTION.getMsg());

        }
    }

    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo memberLoginVo){


        MemberEntity memberEntity= memberService.login(memberLoginVo);
        if (memberEntity!=null){
            System.out.println(memberEntity+".....sucess...................");

            return R.ok().setData(memberEntity);

        }else {
//            System.out.println(memberLoginVo+".....qqqqqqqqqqqqqqqqqqqqqqqq....................");

            return R.error(BigCodeEnume.USERORPASSWORD_EXIST_EXCEPTION.getCode(),BigCodeEnume.USERORPASSWORD_EXIST_EXCEPTION.getMsg());

        }
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
   // @RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
